﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class TodayWorkRegistList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               BindList(1);
            }
        }
        
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = " and TaskStatus = 0";

            Whereclause += " and Convert(char(10),ExpectEndDate,121)='" + System.DateTime.Now.ToString("yyyy-MM-dd") + "'";
           
            int RecordCount = 0;
            List<WsmTaskRegisterEntity> list = new SysUserBll().GetAll<WsmTaskRegisterEntity>(Whereclause, "ExpectEndDate asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = list;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "complete")
            {
                Label RegisterID = RepeaterUsers.Items[e.Item.ItemIndex].FindControl("RegisterID") as Label;
                WsmTaskRegisterEntity entity = new SystemBll().Get<WsmTaskRegisterEntity>(new Guid(RegisterID.Text));
                entity.TaskStatus = 1;
                new SystemBll().Update(entity);
            }

           
            BindList(1);
        }

        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label CustomerID = e.Item.FindControl("CustomerID") as Label;
                Label CustomerName = e.Item.FindControl("CustomerName") as Label;
                Label CustomerAddress = e.Item.FindControl("CustomerAddress") as Label;
                Label PerformPerson = e.Item.FindControl("PerformPerson") as Label;
                Label PerformPersonName = e.Item.FindControl("PerformPersonName") as Label;
                Label TaskType = e.Item.FindControl("TaskType") as Label;
                Label TaskTypeName = e.Item.FindControl("TaskTypeName") as Label;
                Label InstrumentID = e.Item.FindControl("InstrumentID") as Label;
                Label Instrumentname = e.Item.FindControl("Instrumentname") as Label;
                Label IsSystemAssign = e.Item.FindControl("IsSystemAssign") as Label;
                
                if (CustomerID != null)
                {
                    CtrtCustomerEntity entity = new SystemBll().Get<CtrtCustomerEntity>(new Guid(CustomerID.Text));
                    CustomerName.Text = entity.CustomerName;
                    
                    if (PerformPerson.Text.Trim() != "")
                    {
                        PerformPersonName.Text = new SystemBll().Get<SysUserEntity>(new Guid(PerformPerson.Text)).UserName;
                    }
                    TaskTypeName.Text = EnumeratorHelper.GetFieldText((Enumerator.SalesService)Convert.ToInt32(TaskType.Text));
                    if (IsSystemAssign.Text == "False" && (TaskType.Text=="3" || TaskType.Text=="6" || TaskType.Text=="7")) { TaskTypeName.Text += "(应需)"; }
                    if (IsSystemAssign.Text == "True" && (TaskType.Text == "3" || TaskType.Text == "6" || TaskType.Text == "7")) { TaskTypeName.Text += "(计划)"; } 
                    WsmInstrumentInfoEntity insurment = new SystemBll().Get<WsmInstrumentInfoEntity>(new Guid(InstrumentID.Text));
                    if (insurment != null)
                    Instrumentname.Text = insurment.instrumentName;
                }
            }
        }
    }
}
